//
//  XLViewController.m
//  XLCryptoTool
//
//  Created by 10150820867@qq.com on 08/15/2022.
//  Copyright (c) 2022 10150820867@qq.com. All rights reserved.
//
#include <CommonCrypto/CommonCrypto.h>
#import "XLViewController.h"
#import "XLCryptoTool.h"
#import "XLCryptoTool+JFCloudChat.h"

#define AesEncryptKey  @"XLViewController"
#define AesIvKey       @"XLViewController"

@interface XLViewController ()

@end

@implementation XLViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSString *timeString =  @"1692597420129";//@((long)([[NSDate date] timeIntervalSince1970] * 1000)).description;
    NSString *timeStringMD5 = [XLViewController getMD5ForString:timeString].uppercaseString;
    printf("timeStringMD5: %s\n\n", timeStringMD5.UTF8String);
    
    NSString *base64Encode = [XLCryptoTool base64Encode:timeStringMD5];
    printf("base64Encode: %s\n", base64Encode.UTF8String);
    
    NSString *base64Decode = [XLCryptoTool base64Decode:base64Encode];
    printf("base64Decode: %s\n\n", base64Decode.UTF8String);
    
    NSString *token = [XLCryptoTool cloudChatEncryptWithText:timeStringMD5].uppercaseString;
    printf(" token: %s\n", token.UTF8String);
    
    NSString *decode = [XLCryptoTool cloudChatDecryptWithText:token];
    printf("decode: %s\n\n", decode.UTF8String);
    
    NSString *aesEncode = [XLCryptoTool aesEncryptWithKey:AesEncryptKey
                                                 andIvKey:AesIvKey
                                                  andMode:XL_AES_Crypto_Mode_CBC
                                                  andText:timeStringMD5];
    printf("aesEncode: %s\n", aesEncode.UTF8String);
    
    NSString *aesDecode = [XLCryptoTool aesDecryptWithKey:AesEncryptKey
                                                 andIvKey:AesIvKey
                                                  andMode:XL_AES_Crypto_Mode_CBC
                                                  andText:aesEncode];
    printf("aesDecode: %s\n\n", aesDecode.UTF8String);
    
}
+ (NSString *)getMD5ForString:(NSString *)str {
    if (str == nil) {
        return nil;
    }
    const char* input = [str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(input, (CC_LONG)strlen(input), result);
    
    NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [digest appendFormat:@"%02x", result[i]];
    }
    return digest;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
